<?php
	$tabela = new Standings($settings->activeLeague, 1);
	$tabela->getTeams();
	$tabela->getTeamsStats();
	$tabela->sortTeams();
?>

        	<div class="fullPageBox">
            	<div class="statsBox">
                <table class="tabela teamStatistics detailedStats" cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                        <td title="Position" rowspan="2" class="statsPlayerIndex">#</td>
                        <td title="Team Name" rowspan="2" class="statsPlayerName">Team</td>
                        <td title="Free Throws" colspan="3" class="ui-state-default">FT</td>
                        <td title="2 Pointers" colspan="3" class="ui-state-default">2PT</td>
                        <td title="Three Pointers" colspan="3" class="ui-state-default">3PT</td>
                        <td title="Points" rowspan="2">PTS</td>
                        <td title="Assists" rowspan="2">ASS</td>
                        <td title="Defensive Rebounds" rowspan="2">DRB</td>
                        <td title="Offensive Rebounds" rowspan="2">ORB</td>
                        <td title="Blocks" rowspan="2">BLK</td>
                        <td title="Steals" rowspan="2">STL</td>
                        <td title="Fouls" rowspan="2">FLS</td>
                        <td title="BasketLiga Index" rowspan="2">IND</td>
                    </tr>
                    
                    <tr>
                        <td title="Made">M</td>
                        <td title="Attempted">A</td>
                        <td title="Percentage">%</td>
                        <td title="Made">M</td>
                        <td title="Attempted">A</td>
                        <td title="Percentage">%</td>
                        <td title="Made">M</td>
                        <td title="Attempted">A</td>
                        <td title="Percentage">%</td>
                    </tr>
                </thead>
                <tbody>
                
                <?php 
					$i=1;
                    foreach($tabela->tabela as $team) {
						
						$team->getRoster();
						foreach ($team->roster as $player) {
							$player->getAllStats();
							$player->calculateTotalStats();
						}
						$team->stats = $team->getTotalStats();
						
						$total->ft += $team->stats->ft;
						$total->mft += $team->stats->mft;
						$total->fg += $team->stats->fg;
						$total->mfg += $team->stats->mfg;
						$total->threept += $team->stats->threept;
						$total->mtp += $team->stats->mtp;
						$total->pts += $team->stats->pts;
						$total->ast += $team->stats->ast;
						$total->drbd += $team->stats->drbd;
						$total->orbd += $team->stats->orbd;
						$total->blk += $team->stats->blk;
						$total->stl += $team->stats->stl;
						$total->licne += $team->stats->licne;
						$total->BLindex += $team->stats->BLindex;
                ?>
                
                    <tr>
                        <td class="statsPlayerIndex"><?php echo $i++;?></td>
                        <td class="statsPlayerName"><a href="team/<?php echo $team->id; ?>/<?php echo str_replace(" ", "-", $team->ime); ?>"><?php echo $team->ime; ?></a></td>
                        <td><?php echo $team->stats->ft; ?></td>
                        <td><?php echo $team->stats->ft+$team->stats->mft; ?></td>
                        <td><?php echo $team->stats->ft == 0 ? 0 : round(100/($team->stats->mft/$team->stats->ft + 1), 1); ?></td>
                        <td><?php echo $team->stats->fg; ?></td>
                        <td><?php echo $team->stats->fg+$team->stats->mfg; ?></td>
                        <td><?php echo $team->stats->fg == 0 ? 0 : round(100/($team->stats->mfg/$team->stats->fg + 1), 1); ?></td>
                        <td><?php echo $team->stats->threept; ?></td>
                        <td><?php echo $team->stats->threept+$team->stats->mtp; ?></td>
                        <td><?php echo $team->stats->threept == 0 ? 0 : round(100/($team->stats->mtp/$team->stats->threept + 1), 1); ?></td>
                        <td><?php echo $team->stats->pts; ?></td>
                        <td><?php echo $team->stats->ast; ?></td>
                        <td><?php echo $team->stats->drbd; ?></td>
                        <td><?php echo $team->stats->orbd; ?></td>
                        <td><?php echo $team->stats->blk; ?></td>
                        <td><?php echo $team->stats->stl; ?></td>
                        <td><?php echo $team->stats->licne; ?></td>
                        <td><?php echo $team->stats->BLindex; ?></td>
                    </tr>
                <?php 
                    }
                ?>
                </tbody>
                
                <tfoot>
                	<tr class="statsTeamTotal">
                        <td class="statsPlayerIndex"></td>
                        <td class="statsPlayerName">Total</td>
                        <td><?php echo $total->ft; ?></td>
                        <td><?php echo $total->ft+$total->mft; ?></td>
                        <td><?php echo $total->ft == 0 ? 0 : round(100/($total->mft/$total->ft + 1), 1); ?></td>
                        <td><?php echo $total->fg; ?></td>
                        <td><?php echo $total->fg+$total->mfg; ?></td>
                        <td><?php echo $total->fg == 0 ? 0 : round(100/($total->mfg/$total->fg + 1), 1); ?></td>
                        <td><?php echo $total->threept; ?></td>
                        <td><?php echo $total->threept+$total->mtp; ?></td>
                        <td><?php echo $total->threept == 0 ? 0 : round(100/($total->mtp/$total->threept + 1), 1); ?></td>
                        <td><?php echo $total->pts; ?></td>
                        <td><?php echo $total->ast; ?></td>
                        <td><?php echo $total->drbd; ?></td>
                        <td><?php echo $total->orbd; ?></td>
                        <td><?php echo $total->blk; ?></td>
                        <td><?php echo $total->stl; ?></td>
                        <td><?php echo $total->licne; ?></td>
                        <td><?php echo $total->BLindex; ?></td>
                    </tr>
                    <tr role="row">
                        <td class="ui-state-default" title="Position" rowspan="2" class="statsPlayerIndex">#</td>
                        <td title="Player Name" rowspan="2" class="statsPlayerName ui-state-default">Player</td>
                        <td title="Free Throws" colspan="3" class="ui-state-default">FT</td>
                        <td title="2 Pointers" colspan="3" class="ui-state-default">2PT</td>
                        <td title="Three Pointers" colspan="3" class="ui-state-default">3PT</td>
                        <td class="ui-state-default" title="Points" rowspan="2">PTS</td>
                        <td class="ui-state-default" title="Assists" rowspan="2">ASS</td>
                        <td class="ui-state-default" title="Defensive Rebounds" rowspan="2">DRB</td>
                        <td class="ui-state-default" title="Offensive Rebounds" rowspan="2">ORB</td>
                        <td class="ui-state-default" title="Blocks" rowspan="2">BLK</td>
                        <td class="ui-state-default" title="Steals" rowspan="2">STL</td>
                        <td class="ui-state-default" title="Fouls" rowspan="2">FLS</td>
                        <td class="ui-state-default" title="BasketLiga Index" rowspan="2">IND</td>
                    </tr>
                    
                    <tr>
                        <td class="ui-state-default" title="Made">M</td>
                        <td class="ui-state-default" title="Attempted">A</td>
                        <td class="ui-state-default" title="Percentage">%</td>
                        <td class="ui-state-default" title="Made">M</td>
                        <td class="ui-state-default" title="Attempted">A</td>
                        <td class="ui-state-default" title="Percentage">%</td>
                        <td class="ui-state-default" title="Made">M</td>
                        <td class="ui-state-default" title="Attempted">A</td>
                        <td class="ui-state-default" title="Percentage">%</td>
                    </tr>
                </tfoot>

                </table>
                </div>
            </div>

	<script type="text/javascript">             
    $(document).ready(function(){
        $('.tabela').dataTable({
			"sDom": 'R<"H"lfr>t<"F"ip>',
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"fnDrawCallback": function ( oSettings ) {
				/* Need to redo the counters if filtered or sorted */
				if ( oSettings.bSorted || oSettings.bFiltered )
				{
					this.$('td:first-child', {"filter":"applied"}).each( function (i) {
						$(this).html( i+1 );
					} );
				}
			},
			"aoColumnDefs": [
								{ "bSortable": false, "aTargets": [ 0 ] },
								{ "bSearchable": true, "aTargets": [ 1 ] },
							],
			"aaSorting": [[ 18, 'desc' ]],
		});
    });
    </script>